<script setup>
	import { ref } from 'vue'
	import { orderListApi } from '@/services/medicine.js'
	// 获取安全区域尺寸
	const { safeAreaInsets } = uni.getSystemInfoSync()
	const feedTabs = ref([
		{ label: '待支付', status: 10, rendered: false },
		{ label: '待发货', status: 11, rendered: false },
		{ label: '待收货', status: 12, rendered: false },
		{ label: '已完成', status: 13, rendered: false },
		{ label: '已取消', status: 14, rendered: true },
	])
	const more = ref('more')
	const orderList = ref([])
	// 药品订单列表
	async function getOrderList(status) {
		more.value = 'loading'

		// 订单列表接口
		const { code, data, message } = await orderListApi(status)
		// 检测接口是否调用成功
		if (code !== 10000) return uni.utils.toast(message)
		// 渲染列表数据
		orderList.value = data.rows
		// 列表是是否为空
		more.value = orderList.value.length === 0 ? 'noMore' : 'more'
	}
	// 获取药品订单列表
	getOrderList()
	async function scrolltolower() {
		console.log('kvcvfvfv')
	}
	function onTabClick({ index, status }) {
		// 获取订单列表
		getOrderList(status)
	}
</script>
<template>
	<view class="medicine-page">
		<view class="order-status-tabs">
			<custom-sticky :offsetTop="safeAreaInsets.top + 'px'">
				<custom-tabs :list="feedTabs" @changeTab="onTabClick"></custom-tabs>
			</custom-sticky>
		</view>
		<scroll-page @scrolltolower="scrolltolower">
			<view class="order-list">
				<view class="order-list-item" v-for="item in orderList" :key="item.id">
					<view class="order-header">
						<text class="no">订单编号：{{ item.orderNo }}</text>
						<text class="label">待支付</text>
					</view>
					<navigator
						class="medicine-list"
						:url="`/subpkg_medicine/order_detail/index?id=${item.id}`"
						hover-class="none"
					>
						<view
							class="medicine-list-item"
							v-for="medicine in item.medicines"
							:key="medicine.id"
						>
							<view class="content">
								<image
									:src="medicine.avatar"
									mode="aspectFill"
									class="medicine-avator"
								></image>
								<view class="medicine-info">
									<view class="info">
										<text class="name">{{ medicine.name }}</text>
										<text
											class="number"
											:class="{ symbol: medicine.prescriptionFlag === 1 }"
											>{{ medicine.specs }}</text
										>
										<text class="price">¥{{ medicine.amount }}</text>
									</view>
									<view class="quantity"> x{{ medicine.quantity }} </view>
								</view>
							</view>
						</view>
					</navigator>
					<view class="total">
						共2件商品 合计：￥{{ item.actualPayment }}（运费￥{{
							item.couponDeduction
						}}）
					</view>
					<view class="btn">
						<button class="cancel">取消订单</button>
						<button class="going">继续支付</button>
					</view>
				</view>
			</view>
			<!-- 加载状态 -->
			<uni-load-more
				:status="more"
				color="#C3C3C5"
				:icon-size="16"
				:content-text="{
					contentdown: '上拉显示更多',
					contentrefresh: '数据正在加载中',
					contentnomore: '没有更多数据了',
				}"
			/>
		</scroll-page>
	</view>
</template>

<style lang="scss">
	.medicine-page {
		height: 100vh;
		/* #ifdef H5 */
		height: calc(100vh - 44px);
		/* #endif */
		background-color: #f6f6f6;
		box-sizing: border-box;
		padding-bottom: calc(env(safe-area-inset-bottom) + 20rpx);

		display: flex;
		flex-direction: column;
	}

	.order-status-tabs {
		padding: 0rpx 10rpx;
		background-color: #fff;
		:deep(.custom-tabs) {
			justify-content: space-evenly;
		}
	}
	.order-list {
		padding: 20rpx;
		.order-list-item {
			padding: 20rpx 20rpx 0;
			border-radius: 20rpx;
			background-color: #fff;
			.order-header {
				padding: 20rpx;
				display: flex;
				justify-content: space-between;
				border-bottom: 1px solid #f8f8f8;
				padding-bottom: 20rpx;
				.no {
					font-size: 26rpx;
					color: #979797;
				}
				.label {
					font-size: 26rpx;
					color: #979797;
				}
			}
			.medicine-list {
				padding: 30rpx;
				background-color: #fff;
				overflow: hidden;
				.medicine-list-item {
					margin-top: 50rpx;

					&:first-child {
						margin-top: 0;
					}
					&:last-child {
						border: none;
					}
					.content {
						display: flex;
					}

					.medicine-avator {
						width: 180rpx;
						height: 160rpx;
					}
					.medicine-info {
						display: flex;
						justify-content: space-between;

						margin-left: 15rpx;
						flex: 1;
						.info {
							display: flex;
							flex-direction: column;
							justify-content: space-between;
							.name {
								font-size: 30rpx;
								font-weight: 500;
								color: #121826;
							}
							.number {
								margin-top: 20rpx;
								font-size: 24rpx;
								color: #979797;

								&.symbol::before {
									content: '处方药';
									line-height: 1;
									background-color: #31ca9d;
									padding: 5rpx 10rpx;
									color: #fff;

									margin-right: 20rpx;
								}
							}

							.price {
								margin-top: 20rpx;
								color: red;
								font-size: 32rpx;
							}
						}
						.quantity {
							font-size: 30rpx;
							color: #121826;
						}
					}
				}
			}
			.total {
				text-align: right;
				font-size: 24rpx;
				padding: 30rpx 0;
				color: #3c3e42;
			}
			.btn {
				display: flex;
				justify-content: flex-end;
				padding: 20rpx 0;
				.cancel,
				.going {
					border-radius: 100rpx;
					font-size: 26rpx;
					height: 60rpx;
					line-height: 60rpx;
					margin: 0;
				}
				.going {
					border: #31ca9d 1px solid;
					background-color: #fff;
					color: #31ca9d;
					margin-left: 40rpx;
				}
			}
		}
	}
	:deep(.uni-load-more) {
		background-color: #f6f6f6;
	}
	.empty-tips {
		margin-top: 40rpx;
		text-align: center;
		font-size: 28rpx;
		color: #c3c3c5;
	}
</style>
